//
//  ProjHUD.m
//  navi
//
//  Created by 意一yiyi on 2017/7/10.
//  Copyright © 2017年 意一yiyi. All rights reserved.
//

#import "ProjHUD.h"

#import <objc/runtime.h>

#define kMBProgressHUDLabelHeight 59.5

@interface ProjHUD ()

@property (strong, nonatomic) UIView *maskView;
@property (strong, nonatomic) UIActivityIndicatorView *systemHUD;

@property (strong, nonatomic) MBProgressHUD *mbProgressHUD;

@end


@implementation ProjHUD

#pragma mark - SystemHUD

+ (void)showSystemHUDOnView:(UIView *)view {
    
    [self hideSystemHUDOnView:view];
    
    UIView *maskView = [[UIView alloc] initWithFrame:view.bounds];
    maskView.backgroundColor = [UIColor clearColor];
    maskView.tag = 11111111111;
    [view addSubview:maskView];
    
    UIActivityIndicatorView *systemHUD = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:(UIActivityIndicatorViewStyleGray)];
    systemHUD.center = view.center;
    systemHUD.transform = CGAffineTransformMakeScale(1.618, 1.618);
    systemHUD.hidesWhenStopped = YES;
    systemHUD.tag = 11111111112;
    [view addSubview:systemHUD];
    [systemHUD startAnimating];
}

+ (void)hideSystemHUDOnView:(UIView *)view {
    
    UIView *tempMaskView = [view viewWithTag:11111111111];
    UIActivityIndicatorView *tempSystemHUD = [view viewWithTag:11111111112];
    
    if (tempSystemHUD != NULL && tempMaskView != NULL) {
        
        [tempSystemHUD stopAnimating];
        [tempMaskView removeFromSuperview];
    }
}


#pragma mark - MBProgressHUD

+ (void)showMBProgressHUDOnView:(UIView *)view withText:(NSString *)text atPosition:(MBProgressHUDTextPosition)position completionAfterAutohide:(void(^)())completion {
    
    [self hideSystemHUDOnView:view];
    [MBProgressHUD hideHUDForView:view animated:NO];
    
    MBProgressHUD *mbProgressHUD = [MBProgressHUD showHUDAddedTo:view animated:YES];
    mbProgressHUD.mode = MBProgressHUDModeText;
    mbProgressHUD.bezelView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.618];
    mbProgressHUD.contentColor = [UIColor whiteColor];
    
    mbProgressHUD.label.text = text;
        
    switch (position) {
        case MBProgressHUDTextPositionMiddle:
            break;
            
        case MBProgressHUDTextPositionTop:
            mbProgressHUD.offset = CGPointMake(0.0, -CGRectGetHeight(view.frame) / 2.0 + 64 + kMBProgressHUDLabelHeight / 2.0);
            break;
            
        case MBProgressHUDTextPositionBottom:
            mbProgressHUD.offset = CGPointMake(0.0, CGRectGetHeight(view.frame) / 2.0 - 46 - kMBProgressHUDLabelHeight / 2.0);
            break;
            
        default:
            break;
    }
    
    mbProgressHUD.completionBlock = ^{
        
        if (completion) {
            
            completion();
        }
    };
    
    [mbProgressHUD hideAnimated:YES afterDelay:1.618];
}

+ (void)showMBProgressHUDOnView:(UIView *)view withImageName:(NSString *)imageName text:(NSString *)text completionAfterAutohide:(void(^)())completion {
    
    [self hideSystemHUDOnView:view];
    [MBProgressHUD hideHUDForView:view animated:NO];
    
    MBProgressHUD *mbProgressHUD = [MBProgressHUD showHUDAddedTo:view animated:YES];
    mbProgressHUD.mode = MBProgressHUDModeCustomView;
    mbProgressHUD.bezelView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.618];
    mbProgressHUD.contentColor = [UIColor whiteColor];
    
    mbProgressHUD.customView = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    mbProgressHUD.label.text = text;
    
    mbProgressHUD.completionBlock = ^{
        
        if (completion) {
            
            completion();
        }
    };
    
    [mbProgressHUD hideAnimated:YES afterDelay:1.618];
}

+ (void)showMBProgressHUDOnView:(UIView *)view withProgressPrompt:(NSString *)progressPrompt {
    
    [self hideSystemHUDOnView:view];
    [MBProgressHUD hideHUDForView:view animated:NO];
    
    MBProgressHUD *mbProgressHUD = [MBProgressHUD showHUDAddedTo:view animated:YES];
    mbProgressHUD.mode = MBProgressHUDModeAnnularDeterminate;
    mbProgressHUD.bezelView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.618];
    mbProgressHUD.contentColor = [UIColor whiteColor];
    
    mbProgressHUD.label.text = progressPrompt;
}

+ (void)updateMBProgressHUDOnView:(UIView *)view withProgress:(CGFloat)progress progressPrompt:(NSString *)progressPrompt {
    
    MBProgressHUD *tempMBProgressHUD = [MBProgressHUD HUDForView:view];
    
    if ([self respondsToSelector:@selector(showMBProgressHUDOnView: withProgressPrompt:)]) {
        
        tempMBProgressHUD.progress = progress;
        tempMBProgressHUD.label.text = progressPrompt;
    }
}

+ (void)updateMBProgressHUDOnView:(UIView *)view withProgressResult:(MBProgressHUDProgressResult)progressResult progressPrompt:(NSString *)progressPrompt completionAfterAutohide:(void(^)())completion {
    
    MBProgressHUD *tempMBProgressHUD = [MBProgressHUD HUDForView:view];
    
    if ([self respondsToSelector:@selector(showMBProgressHUDOnView: withProgressPrompt:)]) {
        
        tempMBProgressHUD.mode = MBProgressHUDModeCustomView;
        tempMBProgressHUD.label.text = progressPrompt;
        
        if (progressResult == MBProgressHUDProgressResultSuccess) {
            
            tempMBProgressHUD.customView = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:@"MBProgressHUDSuccess"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
        }else if (progressResult == MBProgressHUDProgressResultFail) {
            
            tempMBProgressHUD.customView = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:@"MBProgressHUDFail"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
        }
        
        tempMBProgressHUD.completionBlock = ^{
            
            if (completion) {
                
                completion();
            }
        };
        
        [tempMBProgressHUD hideAnimated:YES afterDelay:1.618];
    }
}

+ (void)showMBProgressHUDOnView:(UIView *)view withGifName:(NSString *)gifName {
    
    [MBProgressHUD hideHUDForView:view animated:NO];
    
    MBProgressHUD *mbProgressHUD = [MBProgressHUD showHUDAddedTo:view animated:YES];
    mbProgressHUD.mode = MBProgressHUDModeCustomView;
    mbProgressHUD.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
    mbProgressHUD.bezelView.backgroundColor = [UIColor clearColor];
    
    NSString *filePath = [[NSBundle mainBundle] pathForResource:gifName ofType:nil];
    NSData *data = [NSData dataWithContentsOfFile:filePath];
    FLAnimatedImageView *gifImageView = [[FLAnimatedImageView alloc] init];
    gifImageView.contentMode = UIViewContentModeScaleAspectFit;
    gifImageView.animatedImage = [FLAnimatedImage animatedImageWithGIFData:data];
    
    mbProgressHUD.customView = gifImageView;
}

+ (void)showMBProgressHUDOnView:(UIView *)view withImageNameArray:(NSArray<NSString *> *)imageNameArray {
    
    [MBProgressHUD hideHUDForView:view animated:NO];
    
    MBProgressHUD *mbProgressHUD = [MBProgressHUD showHUDAddedTo:view animated:YES];
    mbProgressHUD.mode = MBProgressHUDModeCustomView;
    mbProgressHUD.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
    mbProgressHUD.bezelView.backgroundColor = [UIColor clearColor];
    
    UIImageView *gifImageView = [[UIImageView alloc] init];
    NSMutableArray *imageArray = [NSMutableArray array];
    for (NSInteger i = 0; i < imageNameArray.count; i++) {
        
        [imageArray addObject:[UIImage imageNamed:imageNameArray[i]]];
    }
    gifImageView.animationImages = imageArray;
    gifImageView.animationDuration = 1.0;
    gifImageView.animationRepeatCount = 0;
    
    mbProgressHUD.customView = gifImageView;
    [gifImageView startAnimating];
}

+ (void)hideMBProgressHUDOnView:(UIView *)view {
    
    if ([self respondsToSelector:@selector(showMBProgressHUDOnView: withGifName:)] || [self respondsToSelector:@selector(showMBProgressHUDOnView: withImageNameArray:)]) {
        
        MBProgressHUD *tempMBProgressHUD = [MBProgressHUD HUDForView:view];
        [tempMBProgressHUD hideAnimated:YES];
    }
}

@end
